from PIL import Image
import numpy as np
import matplotlib
import os
def img_seg(dir):
    files = os.listdir(dir)
    for file in files:
        a, b = os.path.splitext(file)
        img = Image.open(os.path.join(dir + "\\" + file))
        width, height = img.size
        print(img.size)
        w = 980/7
        h = 500/5
        id = 1
        i = 0
 
        while (i + h <= height):
            j = 0
            while (j + w <= width):
                new_img = img.crop((i, j, i + w, j + w))
                #rename = "D:\\labelme\\images\\"
                #图片保存路径及重命名
                rename = "C:\简单项目\\"
                new_img.save(rename + a + "_" + str(id) + b)
                id += 1
                j += w
            i += h

if __name__ == '__main__':
    #path = "D:\\labelme\\data\\images\\train"
    #图片所在文件夹
    path = "C:\简单项目"
    img_seg(path)
